package com.net.picSpider;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;

/**
 * Created by buguniao on 16/1/12.
 */
public class Test1 {
    public static void main(String[] args){
        new Thread(new ParseTask(1610,5272)).start();
    }

    static class ParseTask implements Runnable{
        private int start;
        private int end;
        PrintWriter writer;

        public ParseTask(int start,int end){
            this.start=start;
            this.end=end;
        }
        public void run() {
            File file = new File("imgs.txt");
            if(!file.exists()){
                try {
                    file.createNewFile();
                    writer = new PrintWriter(new FileWriter(file),true);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            System.out.println(Thread.currentThread().getName()+"["+start+","+end+"]");
            for(int i=start;i<=end;i++){
                System.out.println("开始解析："+i);
                parse("http://www.meizitu.com/a/" + i + ".html");
            }
            System.out.println(Thread.currentThread()+" 线程关闭");
            writer.close();
        }

        public void parse(String url){
            Document doc = null;
            try {
                doc = Jsoup.connect(url).timeout(10000).get();
                String title = doc.title();
                if(title.contains("对不起，出错啦")){
                    System.out.println(url+" 不存在，跳过");
                    return;
                }
                Elements elements = doc.select("img");
                Iterator<Element> it = elements.iterator();

                while(it.hasNext()){
                    String src=it.next().attr("src");
                    if(src.contains("limg.jpg")||src.contains("hezuo")||src.contains("erweima.jpg")){
                        continue;
                    }
                    System.out.println((title+"|"+src).trim());
                    writer.println((title+"|"+src).trim());
                }
            } catch (IOException e) {
                System.out.println(e.getMessage()+" -- 重试");
                parse(url);
            }
        }
    }
}
